package com.dji.compose_learning.ui.view

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Surface
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import com.dji.compose_learning.R
import com.google.accompanist.themeadapter.material3.Mdc3Theme

@Preview
@Composable
fun ShowWithOrigenView() {
    Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
        Column(modifier = Modifier.verticalScroll(rememberScrollState())) {
            Text(text = "this is a compose text.")
            Spacer(modifier = Modifier.height(10.dp))
            AndroidView(factory = {
                LinearLayout(it)
            }, modifier = Modifier
                .fillMaxWidth()
                .padding(10.dp)
                .background(Color.LightGray)) {
                it.orientation = LinearLayout.VERTICAL
                val text1 = TextView(it.context)
                text1.text = "linearlayout text 1"
                val text2 = TextView(it.context)
                text2.text = "linearlayout text 2"
                it.addView(text1)
                it.addView(text2)
                val imageView = ImageView(it.context)
                val linearLayoutParams =
                    LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT)
                linearLayoutParams.topMargin = 20
                imageView.setImageDrawable(it.context.resources.getDrawable(R.drawable.img, null))
                it.addView(imageView, linearLayoutParams)
            }
            val str1 = remember {
                mutableStateOf("this is text1")
            }
            val str2 = remember {
                mutableStateOf("this is text2")
            }
            AndroidView(factory = {
                LayoutInflater.from(it).inflate(R.layout.layout_sample, null, false)
            },
                modifier = Modifier
                    .padding(10.dp)
                    .background(Color.Yellow.copy(alpha = 0.2f))) { view ->
                val parent = view as ViewGroup
                val text1 = parent.findViewById<TextView>(R.id.text1)
                val text2 = parent.findViewById<TextView>(R.id.text2)
                text1.text = str1.value
                text2.text = str2.value

                val innerString = "this is inner string."

                val composeView = parent.findViewById<ComposeView>(R.id.compose_view)
                composeView.setContent {
                    Mdc3Theme { //沿用既有主题到compose
                        Surface(modifier = Modifier.fillMaxSize(),
                            color = Color.Green.copy(alpha = 0.3f)) {
                            Column {
                                Text(text = innerString)
                                Image(painter = painterResource(id = R.drawable.img),
                                    contentDescription = "in in")
                            }
                        }
                    }
                }
            }
        }
    }
}